﻿Imports System.Runtime.InteropServices

Namespace Widgets


    Public Class XboxInfoRetreval(Of TView As {IXboxInfoRetrevalView, Control})
        Inherits WidgetBase(Of TView)

#Region " Varaibles "

        Private m_Timer As New System.Timers.Timer(1000)
        Private m_LastUpdate As DMXIProxy.XboxInfo
        Private m_LatestValue As DMXIProxy.XboxInfo

#End Region

#Region " Setting properties "

        Public Property LastUpdateValue() As String
            Get
                Return My.Settings.LastUpdateValue
            End Get
            Private Set(ByVal value As String)
                My.Settings.LastUpdateValue = value
            End Set
        End Property

        Public Property LastCheck() As DateTime
            Get
                Return My.Settings.LastCheck
            End Get
            Private Set(ByVal value As DateTime)
                My.Settings.LastCheck = value
            End Set
        End Property

        Public Property LastTick() As DateTime
            Get
                Return My.Settings.LastTick
            End Get
            Private Set(ByVal value As DateTime)
                My.Settings.LastTick = value
            End Set
        End Property

#End Region

#Region " Properties "

        Friend Property LastUpdate() As DMXIProxy.XboxInfo
            Get
                Return m_LastUpdate
            End Get
            Private Set(ByVal value As DMXIProxy.XboxInfo)
                m_LastUpdate = value
            End Set
        End Property

        Friend Property LatestValue() As DMXIProxy.XboxInfo
            Get
                Return m_LatestValue
            End Get
            Private Set(ByVal value As DMXIProxy.XboxInfo)
                m_LatestValue = value
            End Set
        End Property

        Public ReadOnly Property TimeBetweenChecks() As TimeSpan
            Get
                Dim Span As TimeSpan = New TimeSpan(0, My.Settings.MinutesBetweenChecks, 0)
                If Span.Ticks > Double.MaxValue Or Span.Ticks < Double.MinValue Then
                    Span = New TimeSpan(0, 10, 0)
                End If
                Return Span
            End Get
        End Property

        Public ReadOnly Property TimeSinceLastCheck() As TimeSpan
            Get
                Return LastTick.Subtract(LastCheck)
            End Get
        End Property

        Public ReadOnly Property TimeSinceLastUpdate() As TimeSpan
            Get
                Return TimeSinceLastCheck.Subtract(TimeBetweenChecks)
            End Get
        End Property

#End Region

#Region " Events & Delegates "

        Friend Delegate Sub XboxInfoRetreval_EventDelegate(ByVal source As XboxInfoRetreval(Of TView), ByVal e As XboxInfoRetrevalEventArgs)

        Friend Delegate Sub OnEventDelegate()

        Friend Event Update As XboxInfoRetreval_EventDelegate
        Friend Event Check As XboxInfoRetreval_EventDelegate
        Friend Event Tick As XboxInfoRetreval_EventDelegate

        Private Sub OnUpdate()
            'If View.InvokeRequired Then
            'View.Invoke(New OnEventDelegate(AddressOf OnUpdate), r)
            'Else
            RaiseEvent Update(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
            'End If
        End Sub

        Private Sub OnCheck()
            'If View.InvokeRequired Then
            'View.Invoke(New OnEventDelegate(AddressOf OnCheck), r)
            'Else
            RaiseEvent Check(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
            'End If
        End Sub


        Private Sub OnTick()
            'If View.InvokeRequired Then
            'View.Invoke(New OnEventDelegate(AddressOf OnTick))
            'Else
            RaiseEvent Tick(Me, New XboxInfoRetrevalEventArgs(LastUpdate, LatestValue, LastCheck, LastTick, TimeBetweenChecks, TimeSinceLastCheck, TimeSinceLastUpdate))
            'End If
        End Sub

#End Region

        Public Sub New(ByVal View As TView)
            MyBase.New(View)
            AddHandler m_Timer.Elapsed, AddressOf m_Timer_Elapsed
            m_Timer.Start()
        End Sub

        Private Sub m_Timer_Elapsed()
            Debug.Write("m_Timer_Elapsed")
            LastTick = Now
            OnTick()
            Debug.Write("Should Check: " & (My.Settings.Enabled And TimeSinceLastUpdate.Ticks > 0).ToString)
            If My.Settings.Enabled And TimeSinceLastUpdate.Ticks > 0 Then
                Debug.Write("is Checking")
                LastCheck = Now
                CheckXboxLiveStatus()
            End If
        End Sub

        Friend Sub CheckXboxLiveStatus()
            Dim c As New DMXIProxy.XboxInfoSoapClient
            Debug.Write("GetXboxInfo")
            LatestValue = c.GetXboxInfo(View.GetGamerTag)
            OnCheck()
            If LastUpdate Is Nothing Then
                LastUpdate = LatestValue
                LastUpdateValue = LatestValue.ToPresenceString
                OnUpdate()
            ElseIf Not LastUpdateValue = LatestValue.ToPresenceString Then
                LastUpdate = LatestValue
                LastUpdateValue = LatestValue.ToPresenceString
                OnUpdate()
            End If
        End Sub

    End Class

End Namespace